package com.company.cloud.mall.modules.app.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.auth.annotation.Login;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.log.annotation.SysLog;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.mall.constant.PayMethodEnum;
import com.company.cloud.mall.constant.ShopConstant.PayConfirmWay;
import com.company.cloud.mall.modules.app.controller.req.Pay2MultipleParam;
import com.company.cloud.mall.modules.app.controller.req.Pay2SingleParam;
import com.company.cloud.mall.modules.app.controller.req.PayResultReportParam;
import com.company.cloud.mall.modules.app.models.PayApplyModel;
import com.company.cloud.mall.modules.app.service.ShopOrderPayService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 支付
 */
@Login
@RestController
@RequestMapping("/app/mall/pay")
@Api(tags = "Shop_支付")
public class OrderPayController {

//	@Autowired
//	private OrdersService ordersService;

    @Autowired
    private ShopOrderPayService shopOrderPayService;

    @PostMapping("/pay2Single")
    @SysLog("支付(单订单)")
    @ApiOperation(value = "支付(单订单)")
    public BaseResult pay2Single(@RequestBody Pay2SingleParam params) {
        ValidatorUtils.validate(params);
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        if (PayMethodEnum.PM_Online_Pay.getCode().equals(params.getPayMethod())) {
            if (StringUtils.isEmpty(params.getPaymentChannel())) {
                throw new BusinessException(BaseReturnEnum.PARAM_NULL);
            }
            System.out.println("+++++++支付(单订单)  sn =" + params.getSn());

            if (params.getSn().indexOf("\"") > 0) {
                String _sn = params.getSn().substring(params.getSn().indexOf("\"") + 1, params.getSn().lastIndexOf("\""));
                System.out.println("+++++++处理后的  sn =" + _sn);
                params.setSn(_sn);
            }

            PayApplyModel data = shopOrderPayService.thirdPay(params.getSn(), params.getPaymentChannel(), params.getReturnUrl(), userInfo);
            return BaseResult.ok(data);
        } else if (PayMethodEnum.PM_Point_Pay.getCode().equals(params.getPayMethod())) {
            shopOrderPayService.payByPoint(params.getSn(), userInfo);
            return BaseResult.ok();
        } else if (PayMethodEnum.PM_Acct_Pay.getCode().equals(params.getPayMethod())) {
            return BaseResult.error();
        } else {
            return BaseResult.error();
        }
    }


    @PostMapping("/pay2Multiple")
    @SysLog("支付(多订单)")
    @ApiOperation(value = "支付(多订单)")
    public BaseResult pay2Multiple(@RequestBody Pay2MultipleParam params) {
        ValidatorUtils.validate(params);
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        if (PayMethodEnum.PM_Online_Pay.getCode().equals(params.getPayMethod())) {
            if (StringUtils.isEmpty(params.getPaymentChannel())) {
                throw new BusinessException(BaseReturnEnum.PARAM_NULL);
            }

            params.getSns().forEach(sn -> {
                System.out.println("+++++++ sn =" + sn);
            });

            PayApplyModel data = shopOrderPayService.thirdPay(params.getSns(), params.getPaymentChannel(), params.getReturnUrl(), userInfo);
            return BaseResult.ok(data);
        } else if (PayMethodEnum.PM_Point_Pay.getCode().equals(params.getPayMethod())) {
            shopOrderPayService.payByPoint(params.getSns(), userInfo);
            return BaseResult.ok();
        } else if (PayMethodEnum.PM_Acct_Pay.getCode().equals(params.getPayMethod())) {
            return BaseResult.error();
        } else {
            return BaseResult.error();
        }

    }

    @PostMapping("/third/payReport")
    @SysLog("第三方支付结果上报")
    @ApiOperation(value = "第三方支付结果上报")
    public BaseResult payResultReport(@RequestBody PayResultReportParam params) {
        ValidatorUtils.validate(params);
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        shopOrderPayService.payReport(params, PayConfirmWay.User_Confirm, userInfo);
        return BaseResult.ok();
    }


}
